//
//  NoteView.swift
//  FastDevelop
//
//  Created by IOS1 on 2025/6/30.
//

import UIKit

class NoteListCell: UITableViewCell {

    var model:Entry?{
        didSet{
            guard let md = model else { return }
            contentLab.text = md.content
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.addSubview(contentLab)
        
        contentLab.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.trailing.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-16)
            make.top.equalToSuperview()
        }
    }
    
    private lazy var contentLab: PaddedLabel = {
        let v = PaddedLabel()
        v.backgroundColor = UIColor(hexString: "#F5F9FA")
        v.font = UIFont.systemFont(ofSize: 14)
        v.textColor = UIColor(hexString: "#8A8B8D")
        v.numberOfLines = 0
        v.padding = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        return v
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class DateHeaderView: UITableViewHeaderFooterView {
         
    var model:DailyLog?{
        didSet{
            guard let md = model else { return }
            timeLabel.text = md.date
            let today = Date()
            let todayStr = today.formattedCalendarDate()
            if md.date == todayStr {
                iconImg.image = UIImage(named: "today")
            }else{
                iconImg.image = UIImage(named: "other_day")
            }
        }
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        backgroundView = UIView()
        backgroundView?.backgroundColor = .clear
        
        addSubviews([
            iconImg,
            timeLabel
        ])

        iconImg.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(32)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
        timeLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(67)
            make.trailing.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
        }
    }
    
    private lazy var iconImg: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        return v
    }()
    private lazy var timeLabel: UILabel = {
        let v = UILabel()
        v.font = UIFont.boldSystemFont(ofSize: 18)
        v.textColor = UIColor(hexString: "#404347")
        return v
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



class PaddedLabel: UILabel {
    var padding: UIEdgeInsets = .zero
    
    override func drawText(in rect: CGRect) {
        super.drawText(in: rect.inset(by: padding))
    }
    
    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(
            width: size.width + padding.left + padding.right,
            height: size.height + padding.top + padding.bottom
        )
    }
    
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        let adjSize = CGSize(
            width: size.width - padding.left - padding.right,
            height: size.height - padding.top - padding.bottom
        )
        let superSize = super.sizeThatFits(adjSize)
        return CGSize(
            width: superSize.width + padding.left + padding.right,
            height: superSize.height + padding.top + padding.bottom
        )
    }
}
